<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="110px">
      <el-form-item label="小区名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入小区名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="登录后台账号" prop="account">
        <el-input
          v-model="queryParams.account"
          placeholder="请输入登录后台账号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:community:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:community:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:community:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['system:community:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="communityList" border size="mini" stripe @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="小区表id" align="center" prop="id" v-if="false"/>
      <el-table-column label="小区名称" align="center" prop="name" />
      <el-table-column label="后台登录账号" align="center" prop="account" />
<!--      <el-table-column label="经度" align="center" prop="lot" />-->
<!--      <el-table-column label="纬度" align="center" prop="lat" />-->
      <el-table-column label="详细地址" align="center" prop="address" />
      <el-table-column label="物业名称" align="center" prop="estateName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:community:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:community:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改小区管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="60%" append-to-body >
      <el-form ref="form" :model="form" :rules="rules"   label-width="120px">
        <el-form-item label="小区名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入小区名称" />
        </el-form-item>
        <el-form-item label="登录后台账号" prop="account" v-if="update">
          <el-input v-model="form.account" disabled placeholder="请输入登录后台账号" />
        </el-form-item>
        <el-form-item label="定位地址" prop="">
          <el-input v-model="locationAddress" disabled placeholder="请定位小区地址" >
            <template #prepend >
              <el-button
                size="mini"
                type="primary" style="background-color: #1c84c6;color: white "
                icon="el-icon-location"
                @click="handleLocation"
              >定位</el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="定位经度" prop="lot">
          <el-input v-model="form.lot" disabled/>
        </el-form-item>
        <el-form-item label="定位纬度" prop="lat">
          <el-input v-model="form.lat"  disabled/>
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入小区详细地址" />
        </el-form-item>
        <el-form-item label="物业名称" prop="estateName">
          <el-input v-model="form.estateName" placeholder="请输入物业名称" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" >
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 打开地图 -->
    <div v-if="openMap" style="width: 60%" >
      <TXMap @cancel="openMap = false" @map-confirm="confirmLocation" @getPlace="getPlace" :visible="openMap" :position="position"></TXMap>
    </div>
  </div>
</template>

<script>
import { listCommunity, getCommunity, delCommunity, addCommunity, updateCommunity, exportCommunity } from "@/api/system/community";
import TXMap from '@/components/TXMap';
export default {
  name: "Community",
  components: {
    TXMap
  },
  data() {
    return {
	  //按钮loading
	  buttonLoading: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      update: false,//是否是修改
      //打开地图
      openMap: false,
      //自动定位的经纬度
      position:this.getLatAndLng(),
      longitudeLatitude:{},//地图经纬度
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 小区管理表格数据
      communityList: [],
      // 弹出层标题
      title: "",
      locationAddress: "",//定位地址
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        account: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        lot: [
          {required: true, message: "请定位小区", trigger: "blur"}
        ],
        name: [
          {required: true, message: "请填写小区名称", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询小区管理列表 */
    getList() {
      this.loading = true;
      listCommunity(this.queryParams).then(response => {
        this.communityList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        account: undefined,
        status: undefined,
        lot: undefined,
        lat: undefined,
        address: undefined,
        estateName: undefined,
      };
      this.resetForm("form");
      this.locationAddress = "";
      this.update = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 打开地图 */
    handleLocation() {
      this.openMap = true;
      this.title = "小区定位";
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    confirmLocation() {
      const e = this.longitudeLatitude
      this.form.lat = parseFloat(e.lat).toFixed(6);
      this.form.lot = parseFloat(e.lng).toFixed(6);
      this.locationAddress = e.addr
      this.form.address = e.addr
      this.openMap = false
    },
    getPlace (e) {
      this.longitudeLatitude = e
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加小区管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getCommunity(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.position={
          latitude:this.form.lat,
          longitude:this.form.lot,
        }
        this.open = true;
        this.update = true;
        this.title = "修改小区管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateCommunity(this.form).then(response => {
              this.buttonLoading = false;
              this.msgSuccess("修改成功");
              this.open = false;
              this.update = false;
              this.getList();
            });
          } else {
            addCommunity(this.form).then(response => {
              this.buttonLoading = false;
              this.msgSuccess("新增成功");
              this.open = false;
              this.update = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除小区管理编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.loading = true;
          return delCommunity(ids);
        }).then(() => {
          this.loading = false;
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有小区管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportCommunity(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
